package it.fantacalcio.spring.service;

import it.fantacalcio.hibernate.dao.UserDAO;
import it.fantacalcio.hibernate.sessionfactory.HibernateSessionFactory;
import it.fantacalcio.model.User;
import it.fantacalcio.utility.Utility;

import java.util.List;

import org.hibernate.Transaction;

public class UserServiceImpl implements UserService {
	
	private UserDAO userDAO;
	
	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
	//----------------------------------------------------------------------------------

	@Override
	public int salva(User user) throws Exception {
		if(user==null){
			return -1;
		}
		try{
			Transaction tx = HibernateSessionFactory.getSession().beginTransaction();
				user.setPassword(Utility.md5hash(user.getPassword()));
				userDAO.save(user);
			tx.commit();
		}catch(Exception e){
			e.printStackTrace();
			return -2;
		}
		return 1;
	}
	
	@Override
	public int cancella(User user) throws Exception {
		if(user==null || user.getId()<1){
			return -1;
		}
		try{
			Transaction tx = HibernateSessionFactory.getSession().beginTransaction();
				userDAO.delete(user);
			tx.commit();
		}catch(Exception e){
			e.printStackTrace();
			return -2;
		}
		return 1;
	}
	
	@Override
	public int modifica(User user) throws Exception {
		if(user==null || user.getId()<1){
			return -1;
		}
		try{
			Transaction tx = HibernateSessionFactory.getSession().beginTransaction();
				userDAO.update(user);
			tx.commit();
		}catch(Exception e){
			e.printStackTrace();
			return -2;
		}
		return 1;		
	}
	
	@Override
	public User get(Long id) throws Exception {
		if(id<1){
			return null;
		}
		Transaction tx = HibernateSessionFactory.getSession().beginTransaction();
		User user = userDAO.get(id);
		tx.commit();
		return user;
	}

	@Override
	public List<User> list() throws Exception {
		Transaction tx = HibernateSessionFactory.getSession().beginTransaction();
		List<User> result=userDAO.list();
		tx.commit();
		return result;
	}
	@Override
	public List<User> listaAttivi() throws Exception {
		Transaction tx = HibernateSessionFactory.getSession().beginTransaction();
		List<User> result=userDAO.listaAttivi();
		tx.commit();
		return result;
	}
	
	@Override
	public List<User> findByExample(User user) throws Exception {
		if(user==null){
			return null;
		}
		Transaction tx = HibernateSessionFactory.getSession().beginTransaction();
		List<User> result=userDAO.findByExample(user);
		tx.commit();
		return result;
	}
	
	@Override
	public User login(String username, String password) throws Exception {
		if(username == null || username.equals("") || password == null || password.equals("")){
			return null;
		}
		password = Utility.md5hash(password);
		User user=userDAO.findByUsernameAndPassword(username, password);
		return user;
	}
	
	@Override
	public List<User> listaPresidenti() throws Exception {
		Transaction tx = HibernateSessionFactory.getSession().beginTransaction();
		List<User> result=userDAO.listaPresidenti();
		tx.commit();
		return result;
	}

}
